package com.leo;

import com.leo.corn.job.CronJob;
import com.leo.listener.MyJobListener;
import com.leo.listener.MySchedulerListener;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;

/**
 * @ClassName ListenerTest
 * @Description
 * @Author wangss
 * @date 2021.07.15 10:41
 * @Version 1.0
 */
public class ListenerTest {

    public static void main(String[] args) throws SchedulerException {
        // 创建schedule实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("scheduler", "schedulerValue");

        // 创建JOb实例
        JobDetail j1 = JobBuilder.newJob(CronJob.class).withIdentity("j1", "g1").build();
        // 创建触发器，每隔2分钟，每天上午8点至下午5点之间
        Trigger t1 = TriggerBuilder.newTrigger().withIdentity("t1", "g1")
                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
                .build();

        // 加入scheduler
        scheduler.scheduleJob(j1, t1);
        MyJobListener myJobListener = new MyJobListener();
        scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.keyEquals(j1.getKey()));

        MySchedulerListener mySchedulerListener = new MySchedulerListener();
        scheduler.getListenerManager().addSchedulerListener(mySchedulerListener);

        scheduler.start();
    }
}
